package de.fraunhofer.scai.lm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;


public class LMProperties extends Properties {

	private static final long serialVersionUID = 1L;

	private String hostName;
	private String trustStore;
	//private String trustStorePassword;
	
	/**
     * Logger for the LMProperties class.
     */
    static Logger logger = Logger.getLogger(LMProperties.class);

    
	/**
	 * Loads the system properties from a file.
	 * 
	 * @param fileName
	 *            the file to load the properties from
	 */
	public void load(String fileName) {
		InputStream is = null;
		// try to load from the users home dir
		try {
			String propfile = fileName;
			String dataDir = System.getProperty("user.home");
			if (dataDir != null) {
				if (!dataDir.endsWith(File.separator))
					dataDir += File.separator;
				propfile = dataDir + propfile;
				logger.debug("system uses props from users home stored in dir " + dataDir);
				logger.info("the used propfile is  " + propfile);
				is = new FileInputStream(propfile);
			}
		} catch  (FileNotFoundException e){
			is = null;
			logger.info("props from users home were not found: " + e);
		}

		// try classpath
		if (is == null) {
			is = ClassLoader.getSystemResourceAsStream(fileName);
			logger.info("system uses props from classpath " + fileName);
		}

		try {
			if (is != null) {
				load(is);
	//			list(System.out);
				hostName = getProperty("hostName");
				trustStore= getProperty("trustStore");
				//trustStorePassword=getProperty("trustStorePassword");
			}
		} catch (IOException e) {
			logger.error("can not load properties : " + e);
			logger.info("    use defaults");
		}
	}

	
	public String getHostName(){
		return hostName;
	}
	
	public String getTrustStore() {
		return trustStore;
	}


/*	public String getTrustStorePassword() {
		return trustStorePassword;
	}*/
}